package com.iecg.websocket;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectEvent;

/**
 * @author niuyy
 * @date 2018/3/26
 */
@Slf4j
@Component
@AllArgsConstructor
public class WebSocketConnectListener implements ApplicationListener<SessionConnectEvent> {

	private final RedisTemplate redisTemplate;

	@Override
	public void onApplicationEvent(SessionConnectEvent event) {
		StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
		String sessionId = sha.getSessionId();
		String name = sha.getUser().getName();
		redisTemplate.opsForValue().set(name, sessionId);
		log.info("sessionId: {} 已连接", sessionId);
	}
}
